package org.KeyCrafter.server.aof;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.concurrent.Future;

public class AofAttribute {
    // 开启aof
    public static final int KeyCrafter_AOF_OFF = 0;
    //关闭aof
    public static final int KeyCrafter_AOF_ON = 1;
    public static final int KeyCrafter_AOF_WAIT_REWRITE = 1;
    int state;
    // AOF缓冲区
    private final ByteBuf buf;
    // 重写 缓冲区
    private ByteBuf rewriteBuf;
    // aof 策略
    private int fsync;
    // 推迟 write 操作的时间
    private long flushPostponedStart;
    // AOF 的当前目标数据库
    int selectedDb;
    // AOF 文件通道
    FileChannel channel;
    // 记录 AOF 的 write 操作被推迟了多少次
    long delayedFsync;
    //最后一次执行 fsync时间
    long lastFsync;
    // 上一次IO异常
    IOException lastWriteEx;
    // AOF 文件的当前字节大小
    long currentSize;
    // 上一次 AOF 执行状态
    int lastWriteStatus;
    //aof 文件路径
    String aofFilename;
    // 负责进行 AOF 重写的线程 ID
    long rewriteThreadId;
    // 执行重写任务的 future
    Future<Integer> rewriteFuture;

    public IOException getLastWriteEx() {
        return lastWriteEx;
    }

    public void setLastWriteEx(IOException ex) {
        this.lastWriteEx = ex;
    }

    public long getCurrentSize() {
        return currentSize;
    }

    public void setCurrentSize(long size) {
        this.currentSize = size;
    }

    public int getLastWriteStatus() {
        return lastWriteStatus;
    }

    public void setLastWriteStatus(int lastWriteStatus) {
        this.lastWriteStatus = lastWriteStatus;
    }
    public ByteBuf getBuf() {
        return buf;
    }

    public int getFsync() {
        return fsync;
    }

    public long getFlushPostponedStart() {
        return flushPostponedStart;
    }

    public int getState() {
        return state;
    }

    public int getSelectedDb() {
        return selectedDb;
    }
    public  FileChannel getChannel() {
        return channel;
    }
    public void setSelectedDb(int selectedDb) {
        this.selectedDb = selectedDb;
    }

    public void setChannel(FileChannel channel) {
        this.channel = channel;
    }

    public void setState(int state) {
        this.state = state;
    }

    public void setFsync(int fsync) {
        this.fsync = fsync;
    }

    public String getAofFilename() {
        return aofFilename;
    }

    public void setAofFilename(String aofFilename) {
        this.aofFilename = aofFilename;
    }

    public void setFlushPostponedStart(long flushPostponedStart) {
        this.flushPostponedStart = flushPostponedStart;
    }

    public AofAttribute() {
        this.selectedDb = -1;
        this.rewriteThreadId = -1;
        this.rewriteBuf = ByteBufAllocator.DEFAULT.directBuffer();
        this.buf = ByteBufAllocator.DEFAULT.directBuffer();
    }


    public ByteBuf getRewriteBuf() {
        return rewriteBuf;
    }

    public Future<Integer> getRewriteFuture() {
        return rewriteFuture;
    }

    public void setRewriteFuture(Future<Integer> rewriteFuture) {
        this.rewriteFuture = rewriteFuture;
    }

    public void incrDelayedFsync() {
        this.delayedFsync++;
    }

    public long getDelayedFsync() {
        return delayedFsync;
    }

    public long getLastFsync() {
        return lastFsync;
    }

    public void setLastFsync(long lastFsync) {
        this.lastFsync = lastFsync;
    }

    public void setDelayedFsync(long delayedFsync) {
        this.delayedFsync = delayedFsync;
    }

    public long getRewriteThreadId() {
        return rewriteThreadId;
    }

    public void setRewriteThreadId(long rewriteThreadId) {
        this.rewriteThreadId = rewriteThreadId;
    }
}
